rm update
[EroBeats.git] / Djinn and Tonic - Erobeats / fileio.cpp
blob344c5d84d8f35a2200a80383ca120edc67afab3c
1 #include "FileIO.h"
3 #include "ScreenNames.h"
4 #include "RescourceKeys.h"
6 FileIO::FileIO()
8 password = "Test";
10 FileIO::FileIO(FileIO* fio)
12 rsc = fio->rsc;
13 password = "Test";
15 FileIO::FileIO(ResourceMaster* rscrc)
17 rsc = rscrc;
18 password = "Test";
22 FileIO::~FileIO()
26 //old methods
28 void FileIO::storeGameText(std::vector<std::string> item) {
29 rsc->gameText = item;
31 //problematic
32 void FileIO::saveUserData(int data, int line) {
33 std::ifstream saveFile("data/save.txt");
34 std::ofstream tempFile("data/temp.txt");
36 std::array<std::string,3> currentData;
37 int counter = 0;
38 while (!saveFile.eof()) {
39 std::string buffer;
40 std::getline(saveFile, buffer);
41 if (buffer != "" && buffer != "\n" && buffer != " ") {
42 currentData.at(counter) = buffer;
43 std::cout << buffer << "\n";
44 counter++;
48 counter = 0;
49 while (counter < 3) {
50 if (line != counter + 1) {
51 tempFile << currentData.at(counter) + "\n";
53 else {
54 tempFile << std::to_string(data) + "\n";
56 counter++;
59 saveFile.close();
60 tempFile.close();
62 std::remove("data/save.txt");
63 std::rename("data/temp.txt", "data/save.txt");
66 void FileIO::saveScoreData(int gamemode, int data, int line) {
67 std::ifstream saveFile("data/scores.txt");
68 std::ofstream tempFile("data/temp.txt");
69 std::array<std::string, 8> currentData;
70 int counter = 0;
71 while (!saveFile.eof()) {
72 std::string buffer;
73 std::getline(saveFile, buffer);
74 if (buffer != "" && buffer != "\n" && buffer != " ") {
75 currentData.at(counter) = buffer;
76 counter++;
77 std::cout << buffer << "\n";
80 std::cout << "skip";
81 counter = 0;
82 while (counter < 8) {
83 if (counter % 2 != gamemode - 1 || counter / 2 + 1 != line) {
84 tempFile << currentData.at(counter) + "\n";
86 else {
87 tempFile << std::to_string(data) + "\n";
89 counter++;
92 saveFile.close();
93 tempFile.close();
95 std::remove("data/scores.txt");
96 std::rename("data/temp.txt", "data/scores.txt");
99 std::vector<std::string> FileIO::loadLanguage(std::string path)
101 std::ifstream langData;
102 langData.open(path);
104 std::string text;
105 std::vector<std::string> result;
106 while (std::getline(langData, text)) {
107 std::cout << text << "\n";
108 if (!text.empty()) {
109 if (text.length() == 1 && text.at(0) != ' ') {
110 result.push_back(text);
112 else if (text.at(0) != '/' && text.at(1) != '/') {
113 result.push_back(text);
117 langData.close();
118 storeGameText(result);
119 return result;
122 int FileIO::loadUserData(int line) {
124 std::string storedData;
125 std::ifstream data;
126 data.open("Data/save.txt");
128 std::array<std::string, 3> currentData;
129 int counter = 0;
130 while (!data.eof()) {
131 std::string buffer;
132 std::getline(data, buffer);
133 if (buffer != "" && buffer != "\n" && buffer != " ") {
134 currentData.at(counter) = buffer;
135 std::cout << buffer << "\n";
136 counter++;
139 data.close();
141 int intStoredData;
142 std::stringstream convert(currentData.at(line - 1));
143 if (!(convert >> intStoredData)) {
144 intStoredData = -1;
145 std::cout << "err\n";
147 return intStoredData;
150 int FileIO::loadScoreData(int gamemode, int line) {
151 std::string storedData;
152 std::ifstream data;
153 data.open("Data/scores.txt");
154 std::array<std::string, 8> currentData;
155 int counter = 0;
157 while (!data.eof()) {
158 std::string buffer;
159 std::getline(data, buffer);
160 if (buffer != "" && buffer != "\n" && buffer != " ") {
161 currentData.at(counter) = buffer;
162 std::cout << buffer;
163 counter++;
166 std::cout << "\t";
168 data.close();
170 if (gamemode == arcade) {
171 line = (line - 1) * 2 + 1;
173 else if (gamemode == life) {
174 line = line * 2;
177 int intStoredData;
178 std::stringstream convert(currentData.at(line - 1));
179 if (!(convert >> intStoredData)) {
180 intStoredData = -1;
181 std::cout << "err\n";
183 return intStoredData;
187 void FileIO::loadMenuItems()
189 std::vector<SDL_Texture*> menuTextures;
191 for (int fileName = LangBG; fileName <= GenericBar; fileName++) {
192 SDL_Texture* sdlText = loadTexture("Art/" + std::to_string(fileName + 1) + ".png");
193 if (sdlText == NULL) {
194 std::cout << SDL_GetError() << " Number " << (fileName + 1) << ".png\n";
196 menuTextures.push_back(sdlText);
198 storeTextures(menuTextures);
201 void FileIO::loadMenuAnimations() {
202 std::vector<std::vector<SDL_Texture*>> menuAnimations;
204 for (int animationFolder = WhiteBlueBG; animationFolder <= WhiteBlueBG; animationFolder++) {
205 std::vector<SDL_Texture*> sdlTextList;
206 for (int i = 1; i <= 57; i++) {
207 sdlTextList.push_back(loadTexture("Art/" + std::to_string(animationFolder) + "/ (" + std::to_string(i) + ").png"));
209 if (sdlTextList.front() == NULL) {
210 std::cout << SDL_GetError();
212 menuAnimations.push_back(sdlTextList);
214 storeAnimations(menuAnimations);
218 void FileIO::loadGameAnimations()
220 std::vector< std::vector<SDL_Texture*>> gameAnimations;
223 void FileIO::loadGameGraphics()
225 std::vector<SDL_Texture*> gameGraphics;
228 SDL_Texture* FileIO::loadTexture(std::string path) {
230 SDL_Texture* newTexture = NULL;
231 SDL_Surface* loadedSurface = IMG_Load(path.c_str());
233 newTexture = SDL_CreateTextureFromSurface(rsc->rendPtr, loadedSurface);
234 SDL_FreeSurface(loadedSurface);
236 return newTexture;
239 //new methods
241 //problematic
242 SDL_Texture* FileIO::loadSpecificImage(std::string path, int image) {
243 return extractSpecificTexture(path, image);
245 std::vector<SDL_Texture*> FileIO::loadSpecificZip(std::string path) {
246 return extractTexturesFromZip(path);
248 void FileIO::loadBGZip() {
249 rsc->gameBG = extractTexturesFromZip("BG.zip");
251 void FileIO::loadTexturesZip() {
252 rsc->gameGraphics = extractTexturesFromZip("Textures.zip");
254 void FileIO::loadAnimationsZip() {
255 rsc->gameAnimations.push_back(extractTexturesFromZip("(0).zip"));
256 /**/
257 for (int i = 1; i <= 36; i++) {
258 rsc->gameAnimations.push_back(extractTexturesFromZip(" (" + std::to_string(i) + ").zip"));
261 void FileIO::loadAnimationsZipUntil(int number) {
262 if (number == 0) {
263 rsc->gameAnimations.push_back(extractTexturesFromZip("(0).zip"));
265 else {
266 for (int i = 1; i <= number; i++) {
267 rsc->gameAnimations.push_back(extractTexturesFromZip(" (" + std::to_string(i) + ").zip"));
271 void FileIO::loadAnimationsZipPast(int number) {
272 if (number == 0) {
273 rsc->gameAnimations.push_back(extractTexturesFromZip("(0).zip"));
275 for (int i = number + 1; i <= 36; i++) {
276 //quick fix for file name issues
277 try {
278 rsc->gameAnimations.push_back(extractTexturesFromZip("(" + std::to_string(i) + ").zip"));
280 catch (std::exception e) {
281 rsc->gameAnimations.push_back(extractTexturesFromZip(" (" + std::to_string(i) + ").zip"));
285 void FileIO::loadAnimationsZipInterval(int start, int end) {
286 if (start == 0) {
287 rsc->gameAnimations.push_back(extractTexturesFromZip("(0).zip"));
288 start++;
290 for (int i = start; i <= end; i++) {
291 //quick fix for file name issues
292 try{
293 rsc->gameAnimations.push_back(extractTexturesFromZip("(" + std::to_string(i) + ").zip"));
295 catch (std::exception e) {
296 rsc->gameAnimations.push_back(extractTexturesFromZip(" (" + std::to_string(i) + ").zip"));
300 void FileIO::loadBGMZip() {
301 rsc->gameBGM = extractBGMZip("BGM.zip");
303 void FileIO::loadSFXZip() {
304 rsc->gameSFX = extractSFXZip("SFX.zip");
307 SDL_Texture* FileIO::extractSpecificTexture(std::string path, int image) {
308 unzFile genericZip = unzOpen(path.c_str());
309 unz_global_info ugi;
310 unzGetGlobalInfo(genericZip, &ugi);
312 SDL_Texture* specificText = NULL;
314 uLong i;
315 std::cout << "Extracting" << "\n";
316 for (i = 0; i < ugi.number_entry; i++) {
317 unz_file_info ufi;
318 char filename[256];
319 unzGetCurrentFileInfo(genericZip, &ufi, filename, sizeof(filename), NULL, 0, NULL, 0);
320 std::string name = getZipFileName(ufi, genericZip);
321 if (name == std::to_string(image)) {
322 uInt size_buf = ufi.uncompressed_size;
323 void*buf = (void*)malloc(size_buf);
324 int err = unzOpenCurrentFilePassword(genericZip, password);
325 if (err != UNZ_OK) {
326 printf("errPass\n");
328 err = unzReadCurrentFile(genericZip, buf, size_buf);
329 if (err < 0) {
330 printf("ErrReadZip\n");
332 SDL_RWops* rw = SDL_RWFromMem(buf, size_buf);
333 if (rw == NULL) {
334 printf("RW\n");
336 SDL_Surface* surf = IMG_Load_RW(rw, 1);
337 if (surf == NULL) {
338 std::cout << "surf\n";
340 specificText = SDL_CreateTextureFromSurface(rsc->rendPtr, surf);
341 if (specificText == NULL) {
342 std::cout << "text\n";
344 SDL_FreeSurface(surf);
345 free(buf);
346 unzClose(genericZip);
347 break;
349 unzGoToNextFile(genericZip);
351 return specificText;
354 std::vector<Mix_Music*> FileIO::extractBGMZip(std::string path) {
355 unzFile bgmZip = unzOpen(path.c_str());
356 unz_global_info ugi;
357 unzGetGlobalInfo(bgmZip, &ugi);
359 std::vector<Mix_Music*> rtnVect(ugi.number_entry);
360 std::vector<Mix_Music*>::iterator it;
362 SDL_RWops* rw = NULL;
363 uLong i;
364 std::cout << "Extracting" << "\n";
365 for (i = 0; i < ugi.number_entry; i++) {
366 unz_file_info ufi;
367 char filename[256];
368 unzGetCurrentFileInfo(bgmZip, &ufi, filename, sizeof(filename), NULL, 0, NULL, 0);
370 uInt size_buf = ufi.uncompressed_size;
371 //std::cout << size_buf << "\n";
372 void* buf = (void*)malloc(size_buf);
373 int err = unzOpenCurrentFilePassword(bgmZip, password);
374 if (err != UNZ_OK)
376 printf("errPass\n");
379 err = unzReadCurrentFile(bgmZip, buf, size_buf);
380 if (err < 0) {
381 std::cout << "errunzBGM\n";
383 rw = SDL_RWFromMem(buf, size_buf);
384 if (rw == NULL) {
385 std::cout << "RW\n";
388 Mix_Music* temp = Mix_LoadMUS_RW(rw, 1);
390 std::string name = getZipFileName(ufi, bgmZip);
391 char chars[] = "()-.wav";
393 for (unsigned int i = 0; i < strlen(chars); ++i)
395 // you need include <algorithm> to use general algorithms like std::remove()
396 name.erase(std::remove(name.begin(), name.end(), chars[i]), name.end());
398 int index = stoi(name) - 1;
400 std::cout << index << "\n";
402 it = rtnVect.begin() + index;
403 rtnVect.at(index) = temp;
404 unzGoToNextFile(bgmZip);
406 unzClose(bgmZip);
407 return rtnVect;
410 std::vector<Mix_Chunk*> FileIO::extractSFXZip(std::string path) {
411 unzFile sfxZip = unzOpen(path.c_str());
412 unz_global_info ugi;
413 unzGetGlobalInfo(sfxZip, &ugi);
415 std::vector<Mix_Chunk*> rtrnVector(ugi.number_entry);
417 SDL_RWops* rw = NULL;
418 uLong i;
419 std::cout << "Extracting" << "\n";
420 for (i = 0; i < ugi.number_entry; i++) {
421 unz_file_info ufi;
422 char filename[256];
423 unzGetCurrentFileInfo(sfxZip, &ufi, filename, sizeof(filename), NULL, 0, NULL, 0);
425 uInt size_buf = ufi.uncompressed_size;
426 //std::cout << size_buf << "\n";
427 void* buf = (void*)malloc(size_buf);
428 int err = unzOpenCurrentFilePassword(sfxZip, password);
429 if (err != UNZ_OK)
431 printf("errPass\n");
434 err = unzReadCurrentFile(sfxZip, buf, size_buf);
435 if (err < 0) {
436 std::cout << "errunzSFX\n";
438 rw = SDL_RWFromMem(buf, size_buf);
439 if (rw == NULL) {
440 std::cout << "RW\n";
442 Mix_Chunk* temp = Mix_LoadWAV_RW(rw, 1);
443 std::string name = getZipFileName(ufi, sfxZip);
444 char chars[] = "()-.wav";
446 for (unsigned int i = 0; i < strlen(chars); ++i)
448 // you need include <algorithm> to use general algorithms like std::remove()
449 name.erase(std::remove(name.begin(), name.end(), chars[i]), name.end());
451 int index = stoi(name) - 1;
453 std::cout << index << "\n";
454 rtrnVector.at(index) = (temp);
456 unzGoToNextFile(sfxZip);
458 unzClose(sfxZip);
459 return rtrnVector;
462 std::vector<SDL_Texture*> FileIO::extractTexturesFromZip(std::string path) {
463 unzFile textureZip = unzOpen(path.c_str());
464 unz_global_info ugi;
465 unzGetGlobalInfo(textureZip, &ugi);
467 std::vector<SDL_Texture*> rtrnVect(ugi.number_entry);
469 uLong i;
470 std::cout << "Extracting" << "\n";
471 for (i = 0; i < ugi.number_entry; i++) {
472 unz_file_info ufi;
473 char filename[256];
474 unzGetCurrentFileInfo(textureZip, &ufi, filename, sizeof(filename), NULL, 0, NULL, 0);
476 uInt size_buf = ufi.uncompressed_size;
477 //std::cout << size_buf << "\n";
478 void* buf = (void*)malloc(size_buf);
479 int err = unzOpenCurrentFilePassword(textureZip, password);
480 if (err != UNZ_OK)
482 printf("errPass\n");
483 int err = unzOpenCurrentFilePassword(textureZip, "test");
486 err = unzReadCurrentFile(textureZip, buf, size_buf);
487 if (err < 0) {
488 std::cout << "errunzTxt\n";
490 SDL_RWops* rw = SDL_RWFromMem(buf, size_buf);
491 if (rw == NULL) {
492 std::cout << "RW\n";
494 SDL_Surface* surf = IMG_Load_RW(rw, 1);
495 if (surf == NULL) {
496 std::cout << "surf\n";
498 SDL_Texture* text = SDL_CreateTextureFromSurface(rsc->rendPtr, surf);
499 if (text == NULL) {
500 std::cout << "text\n";
503 std::string name = getZipFileName(ufi, textureZip);
505 char chars[] = "()-.png";
507 for (unsigned int i = 0; i < strlen(chars); ++i)
509 // you need include <algorithm> to use general algorithms like std::remove()
510 name.erase(std::remove(name.begin(), name.end(), chars[i]), name.end());
512 int index = stoi(name) - 1;
514 std::cout << index << "\n";
516 rtrnVect.at(index) = text;
518 SDL_FreeSurface(surf);
519 free(buf);
521 unzGoToNextFile(textureZip);
523 unzClose(textureZip);
524 return rtrnVect;
527 char* FileIO::getZipFileName(unz_file_info ufi, unzFile uf) {
528 char *fileName = (char *)malloc(ufi.size_filename + 1);
529 unzGetCurrentFileInfo(uf, &ufi, fileName, ufi.size_filename + 1, NULL, 0, NULL, 0);
530 fileName[ufi.size_filename] = '\0';
531 return fileName;
534 void FileIO::eraseUserData()
536 saveUserData(1, LevelUnlocks);
537 saveUserData(0, LevelScores);
538 saveUserData(0, GameSettings);
539 for (int line = 1; line <= 4; line++) {
540 std::cout << "skipyyy";
541 saveScoreData(arcade, 0, line);
542 saveScoreData(life, 0, line);
546 std::vector<int> FileIO::splitInt(std::string str, char delimiter) {
547 std::vector<int> internal;
548 std::stringstream ss(str);
549 std::string result;
551 while (std::getline(ss, result, delimiter)) {
552 int i = atoi(result.c_str());
553 internal.push_back(i);
555 return internal;
557 std::vector<std::string> FileIO::split(std::string str, char delimiter) {
558 std::vector<std::string> internal;
559 std::stringstream ss(str);
560 std::string result;
562 while (std::getline(ss, result, delimiter)) {
563 internal.push_back(result);
565 return internal;